package com.jinzecloud.reportapproval.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinzecloud.reportapproval.bean.DeepStabilityRecord;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jinzecloud.reportapproval.bean.ProductVerificaRecord;
import com.jinzecloud.reportapproval.service.ProductVerificaRecordService;
import com.jinzecloud.reportapproval.utils.PageUtils;
import com.jinzecloud.reportapproval.utils.R;



/**
 * 
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-03 18:28:40
 */
@RestController
@RequestMapping("reportapproval/productverificarecord")
public class ProductVerificaRecordController {
    @Autowired
    private ProductVerificaRecordService productVerificaRecordService;
    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:productverificarecord:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productVerificaRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:productverificarecord:info")
    public R info(@PathVariable("id") Long id){
		ProductVerificaRecord productVerificaRecord = productVerificaRecordService.getById(id);

        return R.ok().put("productVerificaRecord", productVerificaRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:productverificarecord:save")
    public R save(@RequestBody ProductVerificaRecord productVerificaRecord){
//        QueryWrapper<ProductVerificaRecord> temp = new QueryWrapper<ProductVerificaRecord>().eq("project_num", productVerificaRecord.getProjectNum());
//        List<ProductVerificaRecord> list = productVerificaRecordService.list(temp);
//        if(list.size()>0){
//            return R.error("此编号已存在,请勿重复添加");
//        }
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm:ss");
        try {
            Date startDate = inputFormat.parse(productVerificaRecord.getStartDate());
            Date endDate = inputFormat.parse(productVerificaRecord.getEndDate());

            String formattedStartDate = outputFormat.format(startDate);
            String formattedEndDate = outputFormat.format(endDate);

            productVerificaRecord.setStartDate(formattedStartDate);
            productVerificaRecord.setEndDate(formattedEndDate);

            System.out.println("Formatted Start Date: " + productVerificaRecord.getStartDate());
            System.out.println("Formatted End Date: " + productVerificaRecord.getEndDate());
        } catch (ParseException e) {
            e.printStackTrace(); // 处理解析异常
        }
        productVerificaRecord.setAuditStatus(String.valueOf(StateEnum.UN_SUBMIT.getState())).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        productVerificaRecord.setCreatorId(String.valueOf(adminService.getIdByName(authentication.getName())));
		productVerificaRecordService.save(productVerificaRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:productverificarecord:update")
    public R update(@RequestBody ProductVerificaRecord productVerificaRecord){
        // 格式化时间
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mm");
        try {
            Date startDate = inputFormat.parse(productVerificaRecord.getStartDate());
            Date endDate = inputFormat.parse(productVerificaRecord.getEndDate());

            String formattedStartDate = outputFormat.format(startDate);
            String formattedEndDate = outputFormat.format(endDate);
            System.out.println(formattedEndDate);
            productVerificaRecord.setStartDate(formattedStartDate);
            productVerificaRecord.setEndDate(formattedEndDate);

            System.out.println("Formatted Start Date: " + productVerificaRecord.getStartDate());
            System.out.println("Formatted End Date: " + productVerificaRecord.getEndDate());
        } catch (ParseException e) {
            e.printStackTrace(); // 处理解析异常
        }
		productVerificaRecordService.updateById(productVerificaRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("reportapproval:productverificarecord:delete")
    public R delete(@RequestBody Long[] ids){
		productVerificaRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 生成word
     */
    @RequestMapping("/createPdf/{id}")
    public String createWord(@PathVariable Integer id){
        return productVerificaRecordService.createPdf(id);
    }
}
